package cn.org.book.shop.web.controller.article;

import cn.org.book.shop.article.domain.ApArticle;
import cn.org.book.shop.article.domain.ApMaterial;
import cn.org.book.shop.article.service.IApArticleService;
import cn.org.book.shop.article.service.IApMaterialService;
import cn.org.book.shop.common.annotation.Log;
import cn.org.book.shop.common.core.controller.BaseController;
import cn.org.book.shop.common.core.domain.AjaxResult;
import cn.org.book.shop.common.core.page.TableDataInfo;
import cn.org.book.shop.common.enums.BusinessType;
import cn.org.book.shop.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * APP用户素材Controller
 *
 * @author julywhj
 * @date 2020-11-17
 */
@RestController
@RequestMapping("/ap/material")
public class ApMaterialController extends BaseController {
    @Autowired
    private IApMaterialService apMaterialService;

    @Resource
    private IApArticleService apArticleService;

    /**
     * 查询APP用户素材列表
     */
    @PreAuthorize("@ss.hasPermi('ap:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApMaterial apMaterial) {
        startPage();
        List<ApMaterial> list = apMaterialService.selectApMaterialList(apMaterial);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ap:material:list')")
    @GetMapping("/listMaterialByArticleId")
    public AjaxResult listMaterialByArticleId(Long articleId) {
        ApMaterial apMaterial = new ApMaterial();
        apMaterial.setArticleId(articleId);
        return AjaxResult.success(apMaterialService.selectApMaterialList(apMaterial));
    }

    /**
     * 导出APP用户素材列表
     */
    @PreAuthorize("@ss.hasPermi('ap:material:export')")
    @Log(title = "APP用户素材", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ApMaterial apMaterial) {
        List<ApMaterial> list = apMaterialService.selectApMaterialList(apMaterial);
        ExcelUtil<ApMaterial> util = new ExcelUtil<ApMaterial>(ApMaterial.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 获取APP用户素材详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:material:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(apMaterialService.selectApMaterialById(id));
    }

    /**
     * 新增APP用户素材
     */
    @PreAuthorize("@ss.hasPermi('ap:material:add')")
    @Log(title = "APP用户素材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApMaterial apMaterial) {
        AjaxResult ajaxResult;
        return (ajaxResult = saveOrUpdate(apMaterial)) != null ? ajaxResult : toAjax(apMaterialService.insertApMaterial(apMaterial));
    }

    /**
     * 修改APP用户素材
     */
    @PreAuthorize("@ss.hasPermi('ap:material:edit')")
    @Log(title = "APP用户素材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApMaterial apMaterial) {
        AjaxResult ajaxResult;
        return (ajaxResult = saveOrUpdate(apMaterial)) != null ? ajaxResult : toAjax(apMaterialService.updateApMaterial(apMaterial));
    }

    private AjaxResult saveOrUpdate(ApMaterial apMaterial) {
        ApArticle apArticle = apArticleService.selectApArticleById(apMaterial.getArticleId());
        if (apArticle == null) {
            return AjaxResult.error(String.format("文章ID[%s]错误请检查", apMaterial.getArticleId()));
        }
        ApMaterial select = new ApMaterial();
        select.setArticleId(apMaterial.getArticleId());
        select.setOrd(apMaterial.getOrd());
        List<ApMaterial> apMaterials = apMaterialService.selectApMaterialList(select);
        if (apMaterials != null && apMaterials.size() > 0) {
            return AjaxResult.error(String.format("文章ID[%s]和排序[%s]已存在,请删除后在操作", apMaterial.getArticleId(), apMaterial.getOrd()));
        }
        apMaterial.setChannelId(apArticle.getChannelId());
        apMaterial.setMaterialType(apArticle.getLayout());
        apMaterial.setName(apArticle.getTitle().concat("-").concat(String.valueOf(apMaterial.getOrd())));
        return null;
    }

    /**
     * 删除APP用户素材
     */
    @PreAuthorize("@ss.hasPermi('ap:material:remove')")
    @Log(title = "APP用户素材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(apMaterialService.deleteApMaterialByIds(ids));
    }
}
